using System;
using System.Collections;
using System.Collections.Generic;
using com.game.module.test;
using SwanEngine.Astar;
using UnityEngine;

public class MyAstar1
{
    public List<Node> OpenList = new List<Node>();
    public List<Node> CloseList = new List<Node>();

    public Node startPos;
    public Node endPos;

    public Grid grid;

    public Dictionary<string, GridNodeVO> nodeDic = new Dictionary<string, GridNodeVO>();

    public MapDataVo mapData;
    public void Init(MapDataVo vo)
    {
        this.mapData = vo;
        grid = new Grid(vo);
    }

    public List<Vector2> FindPath(Vector2 start, Vector2 end, int cut = 2, bool pathOptimize = true)
    {
        OpenList.Clear();
        CloseList.Clear();

        this.startPos = grid.getNode((int)start.x, (int)start.y);
        this.endPos = grid.getNode((int)end.x, (int)end.y);
        this.startPos.parent = null;
        this.endPos.parent = null;

        CloseList.Add(startPos);
        Serach(startPos);

        //找到路径
        if (CloseList.Count > 1)
        {
            List<Vector2> path = new List<Vector2>();
            Node cloNode = CloseList[CloseList.Count - 1];
            while (cloNode != null)
            {
                path.Add(new Vector2(cloNode.x, cloNode.y));
                SetColor(cloNode, Color.green);
                cloNode = cloNode.parent;
            }
            path.Reverse();
            return path;
        }

        return null;
    }

    public void Serach(Node currNode)
    {
        for (int i = -1; i <= 1; i++)
        {
            for (int j = -1; j <= 1; j++)
            {
                Node node = grid.getNode(currNode.x + i, currNode.y + j);
                if (node != null)
                {
                    if (CloseList.Contains(node)) continue;

                    float cost = 1;
                    if (i != 0 && j != 0) cost = 1.4f;
                    float g = currNode.g + cost;//从起点到当前点的值
                    float h = calcH(node);//从当前节点到终点的估值
                    float f = g + h;
                    if (OpenList.Contains(node))
                    {
                        if (f < node.f)
                        {
                            node.g = g;
                            node.h = h;
                            node.f = f;
                            node.parent = currNode;

                        }
                    }
                    else
                    {
                        node.g = g;
                        node.h = h;
                        node.f = f;
                        node.parent = currNode;
                        OpenList.Add(node);
                    }
                    SetColor(node, Color.grey);
                }
            }
        }

        if (OpenList.Count == 0) return;//没有寻到路径

        Node nearNode = OpenList[0];
        for (int i = 0; i < OpenList.Count; i++)
        {
            if (OpenList[i].f < nearNode.f)
            {
                nearNode = OpenList[i];
            }
        }
        OpenList.Remove(nearNode);
        CloseList.Add(nearNode);
        if (nearNode.x == endPos.x && nearNode.y == endPos.y)
        {
            return;
        }

        Serach(nearNode);
    }

    // public IEnumerator Serach(Node currNode)
    // {
    //     for (int i = -1; i <= 1; i++)
    //     {
    //         for (int j = -1; j <= 1; j++)
    //         {
    //             Node node = grid.getNode(currNode.x + i, currNode.y + j);
    //             if (node != null)
    //             {
    //                 if (CloseList.Contains(node)) continue;

    //                 float cost = 1;
    //                 if (i != 0 && j != 0) cost = 1.4f;
    //                 float g = currNode.g + cost;//从起点到当前点的值
    //                 float h = calcH(node);//从当前节点到终点的估值
    //                 float f = g + h;
    //                 if (OpenList.Contains(node))
    //                 {
    //                     if (f < node.f)
    //                     {
    //                         node.g = g;
    //                         node.h = h;
    //                         node.f = f;
    //                         node.parent = currNode;

    //                     }
    //                 }
    //                 else
    //                 {
    //                     node.g = g;
    //                     node.h = h;
    //                     node.f = f;
    //                     node.parent = currNode;
    //                     OpenList.Add(node);
    //                 }
    //                 SetColor(node, Color.grey);
    //             }
    //         }
    //     }

    //     if (OpenList.Count == 0) yield break;//没有寻到路径

    //     Node nearNode = OpenList[0];
    //     for (int i = 0; i < OpenList.Count; i++)
    //     {
    //         if (OpenList[i].f < nearNode.f)
    //         {
    //             nearNode = OpenList[i];
    //         }
    //     }
    //     OpenList.Remove(nearNode);
    //     CloseList.Add(nearNode);
    //     if (nearNode.x == endPos.x && nearNode.y == endPos.y)
    //     {
    //         //找到路径
    //         if (CloseList.Count > 1)
    //         {
    //             List<Vector2> path = new List<Vector2>();
    //             Node cloNode = CloseList[CloseList.Count - 1];
    //             while (cloNode != null)
    //             {
    //                 path.Add(new Vector2(cloNode.x, cloNode.y));
    //                 SetColor(cloNode, Color.green);
    //                 cloNode = cloNode.parent;
    //             }
    //             path.Reverse();
    //         }
    //         yield break;
    //     }

    //     yield return 1;
    //     CoroutineManager.StartCoroutine(Serach(nearNode));
    // }

    public float calcH(Node currNode)
    {
        return Math.Abs(this.endPos.x - currNode.x) + Math.Abs(this.endPos.y - currNode.y);
    }

    public void SetColor(Node node, Color col)
    {
        GridNodeVO go = null;
        nodeDic.TryGetValue(ToKey(node.x + mapData.offsetX, node.y + mapData.offsetZ), out go);
        if (go != null)
        {
            Material mat = go.go.GetComponent<MeshRenderer>().sharedMaterial;
            if (mat)
            {
                mat.SetColor("_Color", col);
            }
        }
    }

    private static string ToKey(int x, int z)
    {
        return x + "-" + z;
    }
}